import React from 'react'
import apiConfig from '@/core/config/apiConfig'
import RealTimeCodeEditor from '@/components/amis/RealTimeCodeEditor/RealTimeCodeEditor'
import appApplicationStatusEnum from '@/core/application/enums/AppApplicationStatusEnum'
import { HiddenControlSchema } from 'amis/lib/renderers/Form/Hidden'
import { TextControlSchema } from 'amis/lib/renderers/Form/InputText'
import { ListControlSchema } from 'amis/lib/renderers/Form/ListSelect'
import { TextareaControlSchema } from 'amis/lib/renderers/Form/Textarea'
import { DateTimeControlSchema } from 'amis/lib/renderers/Form/InputDate'

import { ApiObject } from 'amis-core/lib/types'
import rsqlUtils from '@/core/utils/rsql/rsqlUtils'
import coreConstants from '@/core/constants/coreConstants'
import { RsqlDataOperator } from '@/core/utils/rsql/types/RsqlDataOperator'
import { RsqlDataOperatorEnum } from '@/core/utils/rsql/types/RsqlOperatorEnum'
import defaultCrudTableUtils from '@/components/DefaultCrudTable/defaultCrudTableUtils'
import { DefaultCrudTableServerApi } from '@/components/DefaultCrudTable/types/DefaultCrudTableProps'

const ${model.className} = () => {
	const entityName = '${model.remark}'
	const isModifyModeExpression = '${"$"}{isEditMode}'
	const modelApiConfig = ${model.apiConfigPrefix}
	let schema = defaultCrudTableUtils.getSchema({
		entityTitle: `${"$"}{entityName}`,
		searchServerApi: {
			apiConfig: modelApiConfig.search,
			requestAdaptor: (api: ApiObject, context: any): ApiObject | Promise<ApiObject> => {
				const operators: RsqlDataOperator[] = [
					<#list model.fields as field>
					{
						operator: RsqlDataOperatorEnum.EQUAL,
						propertyName: '${field.name}'
					} as RsqlDataOperator,
					</#list>
				]
				context[coreConstants.deleteFlagFieldName] = coreConstants.notDeleteFlag
				api = rsqlUtils.addRsqlFilter(api, context, coreConstants.api.queryFilterName, operators)
				return api
			}
		},
		addServerApi: { apiConfig: modelApiConfig.add },
		deleteServerApi: {
			apiConfig: modelApiConfig.removeLogic
		},
		detailServerApi: {
			apiConfig: modelApiConfig.get,
			data: {
				id: '${"$"}{id}'
			}
		},
		editServerApi: {
			apiConfig: modelApiConfig.modifyNotNullField,
			data: {
				<#list model.fields as field>
					<#if field.name == 'createTime'>
					<#elseif field.name == 'deleteFlag'>
					<#elseif field.name == 'updateTime'>
					<#elseif field.name == 'rowVersion'>
					<#else>
				${field.name}: '${"$"}{${field.name}}',
					</#if>
				</#list>
			}
		} as DefaultCrudTableServerApi,
		debug: false,
		formItems: [
			<#list model.fields as field>
				<#if field.idFlag>
			{
				type: 'hidden',
				name: 'id',
				label: 'ID'
			} as HiddenControlSchema,
				<#elseif field.name == 'createTime'>
			{
				type: 'input-datetime',
				name: 'createTime',
				label: '创建时间',
				valueFormat: 'x',
				format: 'YYYY-MM-DD HH:mm:ss',
				static: true,
				visibleOn: isModifyModeExpression
			} as DateTimeControlSchema,
				<#elseif field.name == 'updateTime'>
			{
				type: 'input-datetime',
				name: 'updateTime',
				label: '更新时间',
				valueFormat: 'x',
				format: 'YYYY-MM-DD HH:mm:ss',
				static: true,
				visibleOn: isModifyModeExpression
			} as DateTimeControlSchema,
				<#elseif field.name == 'rowVersion'>
				<#elseif field.name == 'deleteFlag'>
				<#else>
			{
				type: 'input-text',
				name: '${field.name}',
				label: '${field.remark}',
				required: ${field.nullable?string("true","false")},
				disabledOn: isModifyModeExpression
			} as TextControlSchema,
				</#if>
			</#list>
		],
		columns: [
			<#list model.fields as field>
				<#if field.name == 'createTime'>
			{
				type: 'date',
				name: 'createTime',
				label: '创建时间',
				valueFormat: 'x',
				format: 'YYYY-MM-DD HH:mm:ss'
			},
				<#elseif field.name == 'updateTime'>
			{
				type: 'date',
				name: 'updateTime',
				label: '更新时间',
				valueFormat: 'x',
				format: 'YYYY-MM-DD HH:mm:ss'
			},
				<#elseif field.name == 'deleteFlag'>
				<#elseif field.name == 'rowVersion'>
				<#else>
			{
				name: '${field.name}',
				label: '${field.remark}',
				searchable: true
			},
				</#if>
			</#list>
		]
	})
	return <RealTimeCodeEditor code={schema} />
}

export default ${model.className}
